unit fusuariocadastro;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls, DBCtrls, DBGrids, md5, LResources, Buttons;

type

  { TFormUsuarioCadastro }

  TFormUsuarioCadastro = class(TForm)
    ButtonSair: TBitBtn;
    ButtonSalvar: TBitBtn;
    ButtonCancelar: TBitBtn;
    ButtonExcluir: TBitBtn;
    ButtonAlterar: TBitBtn;
    ButtonInserir: TBitBtn;
    ButtonLocalizar: TBitBtn;
    DBEditNome: TDBEdit;
    DBEditLogin: TDBEdit;
    DBNavigator1: TDBNavigator;
    DBRadioGroupTipo: TDBRadioGroup;
    EditConferir: TEdit;
    EditSenha: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    procedure ButtonAlterarClick(Sender: TObject);
    procedure ButtonLocalizarClick(Sender: TObject);
    procedure ButtonCancelarClick(Sender: TObject);
    procedure ButtonExcluirClick(Sender: TObject);
    procedure ButtonSalvarClick(Sender: TObject);
    procedure ButtonInserirClick(Sender: TObject);
    procedure ButtonSairClick(Sender: TObject);
    procedure ControlaButtons(Edicao: boolean);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  FormUsuarioCadastro: TFormUsuarioCadastro;

implementation

uses
  dmusuario;

{$R *.lfm}

{ TFormUsuarioCadastro }

procedure TFormUsuarioCadastro.ButtonInserirClick(Sender: TObject);
begin
  ControlaButtons(True);
  DataUsuario.UsuarioInserir();
  //  Usuario_DataModule.UsuarioInserir();
  DBRadioGroupTipo.ItemIndex := 0;
end;

procedure TFormUsuarioCadastro.ButtonSairClick(Sender: TObject);
begin
  FormUsuarioCadastro.Close();
end;

procedure TFormUsuarioCadastro.ControlaButtons(Edicao: boolean);
begin
  if Edicao then
  begin
    ButtonSalvar.Enabled := True;
    ButtonCancelar.Enabled := True;
    ButtonInserir.Enabled := False;
    ButtonExcluir.Enabled := False;
    ButtonAlterar.Enabled := False;
    ButtonLocalizar.Enabled := False;
    ButtonSair.Enabled := False;
  end
  else
  begin
    ButtonSalvar.Enabled := False;
    ButtonCancelar.Enabled := False;
    ButtonInserir.Enabled := True;
    ButtonLocalizar.Enabled := True;
    ButtonSair.Enabled := True;
    if (DataUsuario.ZQueryUsuario.RecordCount > 0) then
    begin
      ButtonExcluir.Enabled := True;
      ButtonAlterar.Enabled := True;
    end;
  end;
end;

procedure TFormUsuarioCadastro.ButtonAlterarClick(Sender: TObject);
begin
  ControlaButtons(True);
  DataUsuario.UsuarioAlterar();
end;

procedure TFormUsuarioCadastro.ButtonLocalizarClick(Sender: TObject);
begin
  DataUsuario.UsuarioBuscar();
  ControlaButtons(False);
end;

procedure TFormUsuarioCadastro.ButtonCancelarClick(Sender: TObject);
begin
  ControlaButtons(False);
  DataUsuario.UsuarioCancelar();
end;

procedure TFormUsuarioCadastro.ButtonExcluirClick(Sender: TObject);
begin
  if (MessageDlg('Confirmação', 'Deseja excluir o usuário cadastrado?',
    mtConfirmation, [mbYes, mbNo], 0) = mrYes) then
  begin
    DataUsuario.UsuarioExcluir();
    ControlaButtons(False);
  end;
end;

procedure TFormUsuarioCadastro.ButtonSalvarClick(Sender: TObject);
begin
  if EditConferir.Text = EditSenha.Text then
  begin
    DataUsuario.UsuarioFinalizar(MD5Print(MD5String(EditSenha.Text)));
    EditSenha.Clear();
    EditConferir.Clear();
    ControlaButtons(False);
  end
  else
  begin
    ShowMessage('Senhas não conferem');
  end;
end;

end.
